package com.imooc.ad.index;

import com.alibaba.fastjson.JSON;
import com.imooc.ad.dump.DConstant;
import com.imooc.ad.dump.table.*;
import com.imooc.ad.handler.AdLevelDataHandler;
import com.imooc.ad.mysql.constant.OpType;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @ClassName: IndexFileLoader
 * @Author: zhangzhongyu
 * @Date: 2019-10-16 22:22
 * @Version: 1.0
 */
@Component
@DependsOn("dataTable")
public class IndexFileLoader {

    // 根据数据表到处的文件，去读取文件，加载索引

    // init()方法在初始化的时候就应该被加载
    @PostConstruct
    public void init() {

        // 第二层级的全量索引的加载，实现了推广计划的全量数据的加载
        List<String> adPlanStrings = loadDumpData(
                String.format("%s%s",
                        DConstant.DATA_ROOT_DIR,
                        DConstant.AD_PLAN)
        );
        adPlanStrings.forEach(a -> AdLevelDataHandler.handleLevel2(
                JSON.parseObject(a, AdPlanTable.class),
                OpType.ADD
        ));

        // 第二层级的全量索引的加载，实现了创意数据的全量数据的加载
        List<String> adCreativeStrings = loadDumpData(
                String.format("%s%s",
                        DConstant.DATA_ROOT_DIR,
                        DConstant.AD_CREATIVE)
        );
        adCreativeStrings.forEach(b -> AdLevelDataHandler.handleLevel2(
                JSON.parseObject(b, AdCreativeTable.class),
                OpType.ADD
        ));

        // 第三层级的全量索引的加载
        List<String> adUnitStrings = loadDumpData(
                String.format("%s%s",
                        DConstant.DATA_ROOT_DIR,
                        DConstant.AD_UINT)
        );
        adUnitStrings.forEach(c -> AdLevelDataHandler.handleLevel3(
                JSON.parseObject(c, AdUnitTable.class),
                OpType.ADD
        ));

        // 第三层级的全量索引的加载
        List<String> adCreativeUnitStrings = loadDumpData(
                String.format("%s%s",
                        DConstant.DATA_ROOT_DIR,
                        DConstant.AD_CREATIVE_UNIT)
        );
        adCreativeUnitStrings.forEach(d -> AdLevelDataHandler.handleLevel3(
                JSON.parseObject(d, AdCreativeUnitTable.class),
                OpType.ADD
        ));

        // 第四层级的全量索引的加载，地域限制
        List<String> adUnitDistrictStrings = loadDumpData(
                String.format("%s%s",
                        DConstant.DATA_ROOT_DIR,
                        DConstant.AD_UNIT_DISTRICT)
        );
        adUnitDistrictStrings.forEach(d -> AdLevelDataHandler.handleLevel4(
                JSON.parseObject(d, AdUnitDistrictTable.class),
                OpType.ADD
        ));

        // 第四层级的全量索引的加载，itTag限制
        List<String> adUnitItStrings = loadDumpData(
                String.format("%s%s",
                        DConstant.DATA_ROOT_DIR,
                        DConstant.AD_UNIT_IT)
        );
        adUnitItStrings.forEach(d -> AdLevelDataHandler.handleLevel4(
                JSON.parseObject(d, AdUnitItTable.class),
                OpType.ADD
        ));

        // 第四层级的全量索引的加载，keyword限制
        List<String> adUnitKeywordStrings = loadDumpData(
                String.format("%s%s",
                        DConstant.DATA_ROOT_DIR,
                        DConstant.AD_UNIT_KEYWORD)
        );
        adUnitKeywordStrings.forEach(d -> AdLevelDataHandler.handleLevel4(
                JSON.parseObject(d, AdUnitKeywordTable.class),
                OpType.ADD
        ));
    }

    // 对指定文件加载Json的字符串加载成一个List<JSON>的格式
    private List<String> loadDumpData(String fileName) {
        try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))){
            return br.lines().collect(Collectors.toList());
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}
